import { _decorator, Label, Node, NodeEventType } from 'cc';
import { BaseView } from './BaseView';
const { ccclass, property } = _decorator;

export type promptConfig ={
    tittle?:string,
    content?:string,
    confirm?:string,
    cancel?:string,
    hideCancel?:boolean,
    confirmCB?: () => void
}

@ccclass('PromptView')
export class PromptView extends BaseView {
    @property(Label) tittle:Label;
    @property(Label) content:Label;
    @property(Label) confirm:Label;
    @property(Label) cancel:Label;
    @property(Node) cancelBtn:Node;
    @property(Node) confirmBtn:Node;
    private confirmCB: () => void =null;


    init(config:promptConfig) {
        config.tittle && (this.tittle.string = config.tittle);
        config.content && (this.content.string = config.content);
        config.confirm && (this.confirm.string = config.confirm);
        if(config.cancel){
            this.tittle.string = config.cancel;
            this.cancelBtn.active = true;
        }else{
            this.cancelBtn.active = false;
        }
        if(config.confirmCB){
            this.confirmCB = config.confirmCB;
            this.confirmBtn.on(NodeEventType.TOUCH_END,this.confirmCB,this);
        }
        this.confirmBtn.on(NodeEventType.TOUCH_END,this.close,this);
        this.cancelBtn.on(NodeEventType.TOUCH_END,this.close,this);
    }
    close(){
        super.close();
        this.tittle.string = "";
        this.content.string = "";
    }

    onDisable(): void {
        this.cancelBtn.off(NodeEventType.TOUCH_END,this.close,this);
        this.confirmBtn.off(NodeEventType.TOUCH_END,this.close,this);

        if(this.confirmCB){
            this.confirmBtn.off(NodeEventType.TOUCH_END,this.confirmCB,this);
            this.confirmCB = null;
        }
    }

}

